//
//  VideoDownLoadOperation.m
//  JZMCFM
//
//  Created by Mc on 15/11/7.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "VideoDownLoadOperation.h"

@interface VideoDownLoadOperation ()<NSURLSessionDelegate,NSURLSessionDownloadDelegate>
{
    BOOL _isDownLoading;
}
@property(nonatomic,strong)DetailModel2 *downLoadVideoModel;
// 定义session
@property(nonatomic,strong)NSURLSession *currentSession;
// 用于可恢复的下载任务的数据
@property(nonatomic,strong)NSData *partialData;
// 可恢复的下载任务
@property(nonatomic,strong)NSURLSessionDownloadTask *task;

@end

@implementation VideoDownLoadOperation

- (instancetype)initWithDownLoadVideoModel:(DetailModel2 *)videoModel
{
    self = [super init];
    if (self) {
        self.downLoadVideoModel = videoModel;
    }
    return self;
}

- (void)main
{
    NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
    self.currentSession = [NSURLSession sessionWithConfiguration:configure delegate:self delegateQueue:nil];
    self.currentSession.sessionDescription = self.downLoadVideoModel.playUrl64;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.downLoadVideoModel.playUrl64]];
    self.task = [self.currentSession downloadTaskWithRequest:request];
    [self.task resume];
    _isDownLoading = YES;
    while (_isDownLoading) {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
        
    }
    self.partialData = nil;
}

- (void)downLoadPause
{
    [self.task suspend];
}

- (void)downLoadResume
{
    [self.task resume];
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *appendPath = [NSString stringWithFormat:@"/%@.mp4",self.downLoadVideoModel.title];
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:appendPath];
    NSLog(@"%@",file);
    [manager moveItemAtPath:location.path toPath:file error:nil];
    _isDownLoading = NO;
    
    // 下载完成发送通知
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:k_videoDidDownedFinishedSuccess object:self.downLoadVideoModel];
        self.downLoadVideoModel.isDownFinished = YES;
    });
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    self.downLoadVideoModel.progressValue = (double)totalBytesWritten / totalBytesExpectedToWrite;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.updateBlock) {
            self.updateBlock(self.downLoadVideoModel);
        }
    });
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
    
}

@end
